package com.zmytest.annotation;

import javax.validation.Constraint;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import javax.validation.Payload;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * Created on 2018/12/21.
 *
 * @author zhangmingyu
 */
@Target({ElementType.PARAMETER,ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = MyValid.Validator.class)
public @interface MyValid {

    String message() default "";

    Class<?>[] groups() default {};

    Class<? extends Payload>[] payload() default {};


    class Validator implements ConstraintValidator<MyValid,String>{

        @Override
        public void initialize(MyValid myValid) {

        }

        @Override
        public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
            boolean isValid = true;
            if (s != null && !"DESC".equals(s.toUpperCase()) && !"ASC".equals(s.toUpperCase())){
                isValid = false;
            }
            return isValid;
        }
    }
}
